package Class.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDelete {
    public static void main(String[] args) {
        Connection connection = null;  //登入/连接 数据库对象
        Statement statement = null; //执行sql的对象
        try{
            //2.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //3.获取数据库连接对象
            String mysqlurl="jdbc:mysql://localhost:3306/test";
            String username = "root";
            String password = "root";
            connection = DriverManager.getConnection(mysqlurl,username,password);
            //4.获取执行sql语句的对象 Statement
            statement = connection.createStatement();
            //5.定义sql
//            String sql = "update sc set grade=80 where sno='S001'";
//            String sql = "insert into sc values('S002','C002',90)";
//            String sql = "select sno,cno,grade from sc where grade='90'";
            String sql = "delete from sc where sno='S002'";
            //6.执行sql，接收返回
            //查询
//            ResultSet select = statement.executeQuery(sql);
            //更新
            int update = statement.executeUpdate(sql);
            //7.处理结果
            if(update>0){
                System.out.println("数据库连接成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            //8.释放资源
            try {
                if(statement!=null){
                    statement.close();
                }
                if(connection!=null){
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
